package com.sainan114.pipelinestudy.util;


import com.sainan114.pipelinestudy.PipelineStart;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.print.DocFlavor;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lgb
 * @Date: 2021/7/9
 */
public class LogUtils {
    private final static Logger logger = LoggerFactory.getLogger(LogUtils.class);
    private final static StringBuilder res = new StringBuilder();

    private final static String CLASSNAME = "ClassName";
    private final static String EQUAL = "=";
    private final static String SEPARATOR = " | ";
    private final static String LINENUMBER = "lineNumber";
    private final static String COLON = " : ";
    private final static String MSG = "msg";
    private final static String THREADID = "threadId";
    private final static Integer STACKTRACE = 6;

    public static void infoLog(String msg) {
        logger.info(buildContent(msg));
    }

    public static void infoLog(Map<String, String> contentMap) {
        logger.info(buildContent(contentMap));
    }

    public static void warnLog(String msg) {
        logger.warn(buildContent(msg));
    }

    public static void warnLog(Map<String, String> contentMap) {
        logger.warn(buildContent(contentMap));
    }

    public static void errorLog(Map<String, String> contentMap) {
        logger.info(buildContent(contentMap));
    }

    public static void errorLog(String msg) {
        logger.error(buildContent(msg));
    }


    private static String buildContent(String msg) {
        buildCommonContent();
        res.append(MSG).append(COLON).append(msg);
        return res.toString();
    }

    private static String buildContent(Map<String, String> contentMap) {
        buildCommonContent();

        res.append(MSG).append(COLON);

        for (String key : contentMap.keySet()) {
            String value = contentMap.get(key);
            if (value != null) {
                res.append(key).append(EQUAL).append(value).append(SEPARATOR);
            }
        }
        return res.toString();
    }


    private static void buildCommonContent() {
        res.setLength(0);

        res.append(THREADID).append(COLON).append(PipelineStart.threadId.get()).append(SEPARATOR);

        String className = getSimpleClassName();
        if (className != null) {
            res.append(CLASSNAME).append(COLON).append(className).append(SEPARATOR);
        }

        res.append(LINENUMBER).append(COLON).append(getLineNumber()).append(SEPARATOR);


    }


    private static StackTraceElement getStackTrace() {
        StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
        int currentStackTraceOffset = STACKTRACE;
        return stackTrace[currentStackTraceOffset];
    }

    private static Integer getLineNumber() {
        return getStackTrace().getLineNumber();
    }


    private static String getSimpleClassName() {
        String className = getStackTrace().getClassName();

        String simpleClassName = null;
        if (!StringUtils.isBlank(className)) {
            for (int i = className.length() - 1; i >= 0; i--) {
                if (className.charAt(i) == '.') {
                    simpleClassName = className.substring(i + 1);
                    break;
                }
            }
        }

        return simpleClassName;
    }

}
